package com.gimi.cloud.jimi.merchantZone.controller.product;

import java.io.InputStream;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dto.prod.ProdCategoryDTO;
import com.gimi.cloud.jimi.dto.product.ProductStatusDTO;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.product.ProductDto;
import com.gimi.cloud.jimi.dto.params.GetProductParams;
import com.gimi.cloud.jimi.merchantZone.service.product.ProductService;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "product")
public class
ProductCtroller {
	@Autowired
	private ProductService productService;
	private final static Logger logger = LoggerFactory.getLogger(ProductCtroller.class);

	@RequestMapping(value = "/list")
	public void productList(@RequestBody ProductDto pdd,HttpServletResponse response) throws Exception {
		Response resp = new Response();
		PageInfo res = productService.getProductList(pdd);
		resp.setRes_data(res);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
	}
	
	@RequestMapping(value = "/addProduct")
	public void addProduct(@RequestBody ProductDto pdd,HttpServletResponse response) {
		Response resp = new Response();
		ProductDto res = productService.addProduct(pdd);
        resp.setRes_data(null);
        if(res.getResponseCode() != null) {
        	resp.setResponseCode(res.getResponseCode());
        }else {
        	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/updateProduct")
	public void updateProduct(@RequestBody ProductDto pdd,HttpServletResponse response) throws Exception {
		Response resp = new Response();
		ProductDto res = productService.updateProduct(pdd);
        resp.setRes_data(null);
        if(res.getResponseCode() != null) {
        	resp.setResponseCode(res.getResponseCode());
        }else {
        	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/getProduct")
	public void getProduct(@RequestBody GetProductParams params,HttpServletResponse response) throws Exception {
		Response resp = new Response();
		ProductDto dto = productService.getProduct(params.getProdId());
        resp.setRes_data(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/getStoreCats")
	public void getStoreCats(@RequestBody ProductDto params,HttpServletResponse response) {

		Response resp = new Response();
		List<Map<String, Object>> dto = productService.getStoreCats(params);
        resp.setRes_data(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}
	
	@RequestMapping(value = "/releaseProduct")
	public void releaseProduct(@RequestBody ProdCategoryDTO params, HttpServletResponse response) {
		Response resp = new Response();
		Map<String, Object> dto = productService.releaseProduct(params.getProdCatId());
        resp.setRes_data(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
	}

	@RequestMapping(value = "/delete")
	public void deleteProduct(@RequestBody ProductStatusDTO params, HttpServletResponse response) throws Exception {
		Response resp = new Response();
		productService.deleteProduct(params);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}

	@RequestMapping(value = "/offShelf")
	public void offShelfProduct(@RequestBody ProductStatusDTO params, HttpServletResponse response) throws Exception {
		Response resp = new Response();
		productService.offShelfProduct(params);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}

	@RequestMapping(value = "/upShelf")
	public void upShelfProduct(@RequestBody ProductStatusDTO params, HttpServletResponse response) throws Exception {
		Response resp = new Response();
		productService.upShelfProduct(params);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}

	@RequestMapping(value = "/fileUpload")
	public void fileUpload(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {
		long start = System.currentTimeMillis();
		Response resp = new Response();
		//String base64Data = URLDecoder.decode(dto.getImgData(),"utf-8");
		InputStream is = file.getInputStream();//new ByteArrayInputStream(Base64.getDecoder().decode(base64Data));
		String uuId = UUIDUtil.get32UUID();
		String dirPath = "product";
		//String fileName = uuId + "." + dto.getImgType();
		String[] originalFilenames = file.getOriginalFilename().split("\\.");
		String fileName = uuId + "." + originalFilenames[originalFilenames.length-1];
		String result = AliOSSUtil.uploadFile(is,dirPath,fileName);
		Map<String,String> data = new HashMap<>();
		String url = AliOSSUtil.getFileUrl(result,new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 365 * 100));//100years
		System.out.println("==========>上传结果：" + url);
		data.put("url",url);
		logger.info(String.format("===>耗时：%d ms",System.currentTimeMillis() - start));
		resp.setRes_data(data);
		resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.toJson(resp), response);
	}
}
